<style scoped>
span {
  font-size: 15px
}

.dialog-footer button:first-child {
  margin-right: 10px;
}

.el-menu-item is-active {
  color: #535bf2;
  background-color: #1a1a1a;
}


/**/
.el-button--text {
  margin-right: 15px;
}

.el-select {
  width: 300px;
}

.el-input {
  width: 300px;
}

.dialog-footer button:first-child {
  margin-right: 10px;
}


.el-button--text {
  margin-right: 15px;
}

.el-select {
  width: 300px;
}

.el-input {
  width: 300px;
}

.dialog-footer button:first-child {
  margin-right: 10px;
}
</style>

<template>
  <div>
    <el-dialog v-model="centerDialogVisible" title="确认" width="30%" center>
      <template #default>您将删除条目:{{ fromDelete.name }}</template>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="centerDialogVisible = false">取消</el-button>
        <el-button type="danger" :disabled="BtnConfirmAble" @click="deleteRow">确定</el-button>
      </span>
      </template>
    </el-dialog>

    <!--修改弹窗-->
    <el-dialog v-model="addDialogVisible" :title=formAdd.PopupType width="40%" center>
      <el-form :model="formAdd">
        <el-form-item label="账号" :label-width="formLabelWidth"
                      :rules="{required:!formAdd.username,message:'此项必填!'}">
          <el-input :size="inputSize" v-model="formAdd.username" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="密码" :label-width="formLabelWidth"
                      :rules="{required:!formAdd.password,message:'此项必填!'}">
          <el-input :size="inputSize" v-model="formAdd.password" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="姓名" :label-width="formLabelWidth"
                      :rules="{required:!formAdd.name,message:'此项必填!'}">
          <el-input :size="inputSize" v-model="formAdd.name" autocomplete="off"/>
        </el-form-item>

      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="Cancel">取消</el-button>
        <el-button type="primary" :disabled="BtnConfirmAble" @click="ConfirmAdd(formAdd)">确认</el-button>
      </span>
      </template>
    </el-dialog>

    <el-dialog v-model="editDialogVisible" :title=formAdd.PopupType width="40%" center>
      <el-form :model="formEdit">
        <el-form-item label="姓名" :label-width="formLabelWidth"
                      :rules="{required:!formEdit.name,message:'此项必填!'}">
          <el-input :size="inputSize" v-model="formEdit.name" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="电话" :label-width="formLabelWidth"
                      :rules="{required:!formEdit.phone,message:'此项必填!'}">
          <el-input :size="inputSize" v-model="formEdit.phone" autocomplete="off"/>
        </el-form-item>
        <el-form-item label="最大借书量" :label-width="formLabelWidth"
                      :rules="{required:!formEdit.maxNum,message:'此项必填!'}">
          <el-input :size="inputSize" v-model="formEdit.maxNum" autocomplete="off"/>
        </el-form-item>
      </el-form>
      <template #footer>
      <span class="dialog-footer">
        <el-button @click="Cancel">取消</el-button>
        <el-button type="primary" :disabled="BtnEditConfirmAble" @click="ConfirmEdit(formEdit)">确认</el-button>
      </span>
      </template>
    </el-dialog>

    <!--table显示-->
    <div style="margin-bottom: 10px;width: 100%">

      <el-input
          v-model.trim="search.username"
          class="w-50 m-2"
          size="default"
          @keyup.enter="getTableData"
          placeholder="用户名">
      </el-input>
      <el-button style="margin-left: 60px;" size="default" type="primary" @click="getTableData">
        <el-icon>
          <Search/>
        </el-icon>
      </el-button>
    </div>


    <el-scrollbar>
      <el-table v-loading="loading" :header-cell-style="{background:'#eef1f6',color:'#606266'}" border
                :row-style="{height: '54.7px'}"
                style="font-size: 15px" stripe :data="tableData">

        <!--        <el-table-column prop="userPic" label="头像" width="100"/>-->
        <el-table-column prop="id" label="id" width="auto"/>
        <el-table-column prop="nickname" label="昵称" width="auto"/>
        <el-table-column prop="username" label="用户名" width="auto"/>
        <el-table-column prop="createTime" label="创建时间" width="auto"/>
        <el-table-column prop="contact" label="联系方式" width="auto"/>

        <el-table-column fixed="right" label="角色">
          <template #default="scopeEdit">
            <el-tag type="info" v-if="scopeEdit.row.role==='0'">普通用户</el-tag>
            <el-tag type="success" v-if="scopeEdit.row.role==='1'">管理员</el-tag>
          </template>
        </el-table-column>

<!--        <el-table-column prop="role" label="角色" width="auto"/>-->
<!--        <el-table-column fixed="right" label="操作">-->
<!--          <template #default="scopeEdit">-->
<!--            &lt;!&ndash;            <el-button&ndash;&gt;-->
<!--            &lt;!&ndash;                link&ndash;&gt;-->
<!--            &lt;!&ndash;                type="success"&ndash;&gt;-->
<!--            &lt;!&ndash;                size="small"&ndash;&gt;-->
<!--            &lt;!&ndash;                @click.prevent="editInitialize(scopeEdit.row)">&ndash;&gt;-->
<!--            &lt;!&ndash;              <el-icon>&ndash;&gt;-->
<!--            &lt;!&ndash;                <Edit/>&ndash;&gt;-->
<!--            &lt;!&ndash;              </el-icon>&ndash;&gt;-->
<!--            &lt;!&ndash;              <span>编辑</span>&ndash;&gt;-->
<!--            &lt;!&ndash;            </el-button>&ndash;&gt;-->
<!--            <el-button-->
<!--                v-if="scopeEdit.row.role==='0'"-->
<!--                link-->
<!--                type="danger"-->
<!--                size="small"-->
<!--                @click.prevent="deleteLog(scopeEdit.row.id,-->
<!--                scopeEdit.row.username)">-->
<!--              <el-icon>-->
<!--                <Delete/>-->
<!--              </el-icon>-->
<!--              <span>删除</span>-->
<!--            </el-button>-->
<!--          </template>-->
<!--        </el-table-column>-->

      </el-table>
    </el-scrollbar>
    <el-pagination v-model:currentPage="page.current"
                   layout="sizes, total,prev, pager, next, jumper"
                   :total="page.total"
                   v-model:page-size="page.limit"
                   :page-sizes="[10, 15, 20, 25]"
                   @current-change="handleCurrentChange"
                   @size-change="handleCurrentChange"
    >
    </el-pagination>
  </div>
</template>

<script lang="ts" setup>

import {onMounted, reactive, ref, watch} from "vue";
import {Delete, Edit, Operation, Search} from '@element-plus/icons-vue'

import {
  editApi,
  deleteApi,
  getOneByIdApi,
  searchAllApi, addAdminApi,
} from "@/api/user";
import {ElMessage} from "element-plus";
import {ElLoading} from "element-plus/es";
import {searchTypesApi} from "@/api/goods";

const centerDialogVisible = ref(false)

//搜索功能
const inputSearch = ref([])


//数据初始化
onMounted(() => {
  getTableData()
})

const rolesList = ref<any[]>([])
//分页
const page = reactive({
  current: 1,
  limit: 10,
  total: 0,
})


//分页操作
function handleCurrentChange(e: any) {
  getTableData()
}

const tableData = ref([])
const loading = ref(true)
const search = reactive({
  name: "",
  username: "",
})

function getTableData() {
  searchAllApi({username: search.username}).then((response: any) => {
    if (response.code === 200) {
      tableData.value = response.data
      page.total = response.data.total
    } else {
      ElMessage.warning(response.msg)
    }
    loading.value = false
  }).catch(function (res) {
    loading.value = false
    ElMessage.error("页面加载失败")
  })
  searchTypesApi().then((response: any) => {
    if (response.code === 200) {
      typesList.value = response.data.typelist
    } else {
      ElMessage.warning(response.message)
    }
  })
}


//添加修改数据初始化
const BtnConfirmAble = ref(true)


const formAdd = reactive({
  username: "",
  name: "",
  password: "",
  PopupType: ""
})


//修改字段检测
watch(() => ({...formAdd}), (count, prevCount) => {
  BtnConfirmAble.value =
      count.username == "" ||
      count.name == "" ||
      count.password == "" ||
      count.PopupType == "";
})


function ConfirmAdd(e: any) {
  const loading = ElLoading.service({
    lock: true,
    text: 'Loading',
    background: 'rgba(0,0,0,0.7)',
  })
  BtnConfirmAble.value = true
  addAdminApi(e)
      .then(function (response: any) {
        if (response.code === 200) {
          getTableData()
          addDialogVisible.value = false;
          ElMessage.success(response.msg)
        } else {
          ElMessage.warning(response.msg)
        }
        loading.close()
      }).catch(response => {
    ElMessage.error("修改失败")
    loading.close()
  })

}

const BtnEditConfirmAble = ref(true)

const formEdit = reactive({
  lendNum: 0,
  maxNum: 0,
  name: "",
  phone: "",
  role: 0,
  status: 0,
  times: 0,
  uid: "",
  PopupType: ""
})

const editDialogVisible = ref(false)

//弹窗展示编辑表格
function editInitialize(book: any) {
  formEdit.lendNum = book.lendNum;
  formEdit.maxNum = book.maxNum;
  formEdit.name = book.name;
  formEdit.phone = book.phone;
  formEdit.role = book.role;
  formEdit.status = book.status;
  formEdit.times = book.times;
  formEdit.uid = book.uid;
  formEdit.PopupType = PopupType.value[1]
  editDialogVisible.value = true;
}


//修改字段检测
watch(() => ({...formEdit}), (count, prevCount) => {
  BtnEditConfirmAble.value =
      count.name == "" ||
      count.PopupType == "";
})

function ConfirmEdit(e: any) {
  const loading = ElLoading.service({
    lock: true,
    text: 'Loading',
    background: 'rgba(0,0,0,0.7)',
  })
  BtnEditConfirmAble.value = true
  editApi(e)
      .then(function (response: any) {
        if (response.code === 200) {
          getTableData()
          editDialogVisible.value = false;
          ElMessage.success(response.msg)
        } else {
          ElMessage.warning(response.msg)
        }
        loading.close()
      }).catch(response => {
    ElMessage.error("修改失败")
    loading.close()
  })

}

function Cancel() {
  addDialogVisible.value = false;
}


//添加系统用户
const addDialogVisible = ref(false)


const PopupType = ref(['添加', '编辑'])
const typesList = ref<any[]>([])

function addInitialize() {
  formAdd.username = "";
  formAdd.name = "";
  formAdd.password = "";
  formAdd.PopupType = PopupType.value[0];
  addDialogVisible.value = true;
}

const fromDelete = reactive({
  id: -1,
  name: ''
})

//删除
function deleteLog(id: any, name: any) {
  fromDelete.id = id
  fromDelete.name = name
  centerDialogVisible.value = true;
  BtnConfirmAble.value = false;
}

function deleteRow() {
  const loading = ElLoading.service({
    lock: true,
    text: 'Loading',
    background: 'rgba(0,0,0,0.7)',
  })
  BtnConfirmAble.value = true
  deleteApi(fromDelete.id)
      .then(function (response: any) {
        if (response.code === 200) {
          getTableData()
          centerDialogVisible.value = false
          ElMessage.success("删除成功")
        }
        loading.close()
      }).catch(response => {
    ElMessage.error("删除失败")
    loading.close()
  })
}

const formLabelWidth = '140px'
const inputSize = 'default'

</script>
